package com.yiwei.zhh.activity;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yiwei.zhh.R;
import com.yiwei.zhh.api.Api;
import com.yiwei.zhh.api.bean.IncomeCertV3Bean;
import com.yiwei.zhh.api.bean.IncomeHouseBean;
import com.yiwei.zhh.api.bean.UploadImageBean;
import com.yiwei.zhh.util.GlideRoundTransform;
import com.yiwei.zhh.util.ImageUtil;
import com.yiwei.zhh.util.Network;
import com.yiwei.zhh.util.RxLifeUtil;
import com.yiwei.zhh.util.ToastUtil;
import com.yiwei.zhh.view.V19FrameLayout;
import com.yiwei.zhh.view.uploadImage.UploadImgAdapter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/6/10.
 */

public class V3HouseIdentityActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.img_identity_one)
    ImageView imgIdentityOne;
    @BindView(R.id.btn_take_one)
    ImageView btnTakeOne;
    @BindView(R.id.img_identity_two)
    ImageView imgIdentityTwo;
    @BindView(R.id.btn_take_two)
    ImageView btnTakeTwo;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    private Unbinder unbinder;
    private UploadImgAdapter uploadImgAdapter;
    private int take_type = 0;
    private boolean is_one_ok = false;
    private boolean is_two_ok = false;
    private String one_url;
    private String two_url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_house_identity_v3);
        initBar();
        unbinder = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("房产证明");
    }

    private void goToTakePhoto() {
        checkPermission(new CheckPermListener() {
            @Override
            public void superPermission() {
                if (uploadImgAdapter == null) {
                    initUploadImgAdapter();
                }
                uploadImgAdapter.withMode(UploadImgAdapter.MODE_IMG).showDialog();
            }

            @Override
            public void superReject() {

            }
        }, R.string.camera_storage_card_p, R.string.get_cammra, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE);
    }

    private void initUploadImgAdapter() {
        uploadImgAdapter = new UploadImgAdapter(this, new UploadImgAdapter.Action2() {
            @Override
            public void call(Bitmap file, int mode) {
                if (mode == UploadImgAdapter.MODE_IMG) {
                    uploadImg(file);
                }
            }
        });
    }

    private void getIncomeHouse(String img1, String img2) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        loadingDialog.show();
        IncomeHouseBean bean = new IncomeHouseBean();
        bean.image = img1;
        bean.image2 = img2;
        Api.api_service.getIncomeHouseV3(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "上传成功");
                    finish();
                });
    }

    public void uploadImg(Bitmap img) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        loadingDialog.show();

        UploadImageBean bean = new UploadImageBean();
        bean.img = ImageUtil.Bitmap2StrByBase64(img, 100);
        Api.api_service.uploadImg(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    if (take_type == 0) {
                        one_url = result.getUrl();
                        Glide.with(this).load(result.getUrl()).transform(new GlideRoundTransform(this, 5)).into(imgIdentityOne);
                        is_one_ok = true;

                    } else {
                        two_url = result.getUrl();
                        Glide.with(this).load(result.getUrl()).transform(new GlideRoundTransform(this, 5)).into(imgIdentityTwo);
                        is_two_ok = true;
                    }
                    if (is_two_ok && is_one_ok) {
                        btnSubmit.setSelected(true);
                    } else {
                        btnSubmit.setSelected(false);
                    }
                });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (uploadImgAdapter != null) {
            uploadImgAdapter.onActivityResult(requestCode, resultCode, data);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        unbinder = null;
    }

    @OnClick({R.id.btn_back, R.id.btn_take_one, R.id.btn_take_two, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_take_one:
                take_type = 0;
                goToTakePhoto();
                break;
            case R.id.btn_take_two:
                take_type = 1;
                goToTakePhoto();
                break;
            case R.id.btn_submit:
                if (!is_one_ok) {
                    ToastUtil.makeText(this, "请上传第一张房产证明");
                    return;
                }
                if (!is_two_ok) {
                    ToastUtil.makeText(this, "请上传第二张房产证明");
                    return;
                }
                getIncomeHouse(one_url, two_url);
                break;
        }
    }
}
